<template>
	<view class="body">
		<u-popup :show="show" @close="close" mode='center' :round="10">
		  		<view style="padding: 20rpx 20rpx;display: flex;justify-content: space-around;gap: 20rpx;">
					<u-button type="primary" text="确定" :disabled="formData.is_affirm === '1' ? true : false "  @click='gridSaveData'></u-button>
					<u-button type="primary" text="忽略" :disabled="formData.is_ignore === '1' ? true : false " @click = 'ignoreData'></u-button>
					<u-button type="primary" text="转工单" :disabled="formData.is_turn_to_order === '1' ? true : false "  @click = 'workData'></u-button>
				</view>
		</u-popup>
		<xk-header :back="false" title="消息"></xk-header>
		<view class="util-bar">

		</view>
		<scroll-view class="scroll-view" :style="'height:'+scrollViewHeight + 'px'" scroll-y @scrolltolower="loadMoreData" >
		    <uni-card v-for="(item, index) in messageData" :key="index" @click="openDialog(item)">
					<view class="scroll-item">
						<view class="sub">告警名称:</view>
						<view style="color: #D42C26;">{{ item.alarm_records_name || '暂无名称'}}</view>
					</view>
					<view class="scroll-item">
						<view class="sub">告警描述:</view>
						<view>{{ item.alarm_description || '暂无描述'}}</view>
					</view>
					<view class="scroll-item">
						<view class="sub">告警等级:</view>
						<view>{{ alarmLevelMap[item.alarm_level]  || '轻微告警'}}</view>
					</view>
					<view class="scroll-item">
						<view class="sub">是否转工单:</view>
						<view>{{ item.is_turn_to_order === "1" ? '是' : '否' }}</view>
					</view>
					<view class="scroll-item">
						<view class="sub">是否确认:</view>
						<view>{{ item.is_affirm === "1" ? '是' : '否' }}</view>
					</view>
					<view class="scroll-item">
						<view class="sub">是否忽略:</view>
						<view>{{ item.is_ignore === "1" ? '是' : '否' }}</view>
					</view>
			</uni-card>
		  </scroll-view>




		  <!-- 后续优化加loadmore -->
		<!-- <view v-if="messageData.length == 0" class="empty-view">
			<image class="empty-icon" src="/static/empty_data_icon.png"></image>
			<view>哎呀，该界面暂无内容~</view>
		</view> -->

		<!-- <view class="util-bar">
			<view class="menu" >
				<view class="menu-line" v-for="(menuItem, menuIndex) of menuList" :key="menuIndex" :style="{marginTop: menuIndex>0 ? '30rpx': '0rpx'}">
					<view class="menu-item" v-for="(item, index) of menuItem" :key="index"  style="background-color: #fff;"
						@click="menuClick(item)">
						<image class="menu-item-img" :src="item.icon"/>
						<text class="text">{{item.title}}</text>
						<view v-if="item.num > 0" class="menu-num">{{item.num}}</view>
					</view>
				</view>
			</view>
		</view> -->
	<!-- 	<view class="info-list">
			<view class="info-item" v-for="(item, index) of deviceMsgList" :key="index">
				<view class="title">
					<view>设备告警</view> -->
					<!-- <image src="/static/shezhi.png"></image> -->
			<!-- 	</view>
				<view class="sub-title">{{item.event_describe}}</view>
				<view class="content">
					<text>通知内容：</text><text class="red">{{item.notify_content || '无'}}</text>
				</view>
				<view class="content" style="display:flex;align-items: center; margin-top: 20rpx;">
					<uni-icons type="location-filled" size="20" style="color:#2C72FF"></uni-icons>
					<text>{{item.address}}</text>
				</view>
				<view class="bottom">
					<view>{{item.event_time}}</view>
					<view class="btn">
						<view v-if="$util.getRights().pd_device_repair_apply" class="common-btn btn-plant" @click="handleMsg(item.id, '0')">忽略</view>
						<view v-if="$util.getRights().pd_device_repair_apply" class="common-btn" @click="handleMsg(item.id, '1')">响应</view>
					</view>
				</view>
			</view> -->
			<!-- <view class="info-item">
				<view class="title">
					<view>物业消息</view>
					<image src="/static/shezhi.png"></image>
				</view>
				<view class="sub-title">2020-2021年度物业缴费通知</view>
				<view class="content">
					<view class="content-item"><view class="key">业主姓名：</view>黄正零</view>
					<view class="content-item"><view class="key">业主房号：</view>A栋3208房</view>
					<view class="content-item red"><view class="key">拖欠费用：</view>￥360.00</view>
					<view class="content-item"><view class="key">费用类型：</view>电费</view>
					<view class="content-item"><view class="key">应缴费日期：</view>2020.10.01</view>

				</view>
				<view class="bottom">
					<view>2021.05.17  09:30:46</view>
					<view class="btn">查看明细</view>
				</view>
			</view> -->

			<!-- <view v-if="deviceMsgList.length == 0" class="empty-view">
				<image class="empty-icon" src="/static/empty_data_icon.png"></image>
				<view>哎呀，该界面暂无内容~</view>
			</view> -->
		<!-- </view> -->
	</view>
</template>

<script>
export default {
	data() {
		return {
			show:false,
			alarmLevelMap:{
				'0':'轻微告警',
				'1':'一般告警',
				'2':'严重告警'
			},
			menuList: [[{	// 租户菜单
				title: "日常消息",
				icon: "/static/msg-rcxx.png",
				url: "",
				num: 0
			},{
				title: "物业消息",
				icon: "/static/msg-wyxx.png",
				url: "",
				num: 0
			},{
				title: "系统消息",
				icon: "/static/msg-xtxx.png",
				url: "",
				num: 0
			}]],
			deviceMsgList: [],
			page: 0,
			limit: 30,
			messageData :[],
			pageNum: 1,
			scrollViewHeight: 0,
			formData:{}
		};
	},
	mounted() {
		const {windowHeight} = uni.getSystemInfoSync()
		console.log(2222,windowHeight)
		this.scrollViewHeight = windowHeight
		// this.$util.getHeightWithTopEl(".util-bar").then(height => {
		// 	this.scrollViewHeight = height;
		// });

		this.fetchNextPageData()
	},
	onLoad() {
		// #ifdef APP-VUE
			plus.push.clear();//清空所有推送消息
		// #endif

	},
	onShow(){
		this.page = 0;
		this.isAll = false
		this.getDeviceWarnMsg()
	},
	onReachBottom(){
		this.getDeviceWarnMsg()
	},
	methods:{
		openDialog(listContent){
			this.show = true
			console.log('listContent',listContent)
			this.formData = listContent
			// console.log(11111,this.show)
		},
		gridSaveData(){
			this.$util.post(
			{
				// url:'msgCenterService/msg/gridsave',
				url:'wx/alarmMsg/updateAlarmMsgStatus',
					data:{
						type:'sure',
						alarm_id:this.formData?.select_id
						}
				}
			).then(res=>{
				console.log(res)
				this.updateFetchNextPageData()
				this.show = false
			})
		},
		ignoreData(){
			this.$util.post(
			{
				// url:'msgCenterService/msg/gridsave',
				url:'wx/alarmMsg/updateAlarmMsgStatus',
					data:{
						type:'neglect',
						alarm_id:this.formData?.select_id
						}
				}
			).then(res=>{
				console.log(res)
				this.updateFetchNextPageData()
				this.show = false
			})
		},
		workData(){
			this.$util.post(
						{
							// url:'msgCenterService/msg/gridsave',
							url:'wx/alarmMsg/updateAlarmMsgStatus',
								data:{
									type:'repair',
									alarm_id:this.formData?.select_id
									}
							}
						).then(res=>{
							console.log(res)
							this.updateFetchNextPageData()
							this.show = false
						})
		},
		close(){
			this.show =false
		},
		async loadMoreData() {
			 this.pageNum++
			 console.log(this.pageNum)
			 // const nextPageData =  this.fetchNextPageData()
			 // console.log('333',nextPageData)
			 // this.messageData = this.messageData.concat(nextPageData)
			 // console.log('2123',this.messageData)
			 await this.fetchNextPageData()

		 },
		 //刷新当前页修改是否确认方法

		 updateFetchNextPageData(){
			 let that = this
			  //告警总次数
			  uni.request({
			 	url:`${this.$api.ibmsBaseUrl}/msgCenterService/msg/list`,
			 	data:{
			 		  funId: "msg_alarm_records",
			 		  warnStatus: "",
			 		  name: "",
			 		  pageNo: 1,
			 		  offset:0,
			 		  limit: that.pageNum * 5,
			 		  baseSQL: ""
			 	},
			  	// url:`http://192.168.1.114:8098/msg/cockpit/queryAlarm?limit=${this.pageNum}`,
			  	method: 'POST',
			  	header: {
			  	     'Content-Type': 'application/json' // 请求头设置为json格式
			  	   },
			  	   success: function(res) {
			  		   that.messageData = res.data.rows
			 		   // console.log('请求成功返回的值',that.messageData,res.data.rows); // 请求成功的回调函数，输出响应数据
			  	   },
			  	   fail: function(err) {
			  	     console.log(err); // 请求失败的回调函数，输出错误信息
			  	   }
			  })
			 return that.messageData
		 },

		 fetchNextPageData(){
			 let that = this
			 //告警总次数
			 const dataObj = {
				 funId: "msg_alarm_records",
				 warnStatus: "",
				 name: "",
				 pageNo: that.pageNum,
				 offset:0,
				 limit: 5,
				 baseSQL: ""
			 }
			 uni.request({
				url:`${this.$api.ibmsBaseUrl}/msgCenterService/msg/list`,
				data:dataObj,
			 	// url:`http://192.168.1.114:8098/msg/cockpit/queryAlarm?limit=${this.pageNum}`,
			 	method: 'POST',
			 	header: {
			 	     'Content-Type': 'application/json' // 请求头设置为json格式
			 	   },
			 	   success: function(res) {

			 		   that.messageData.push(...res.data.rows)
					   // console.log('请求成功返回的值',that.messageData,res.data.rows); // 请求成功的回调函数，输出响应数据
			 	   },
			 	   fail: function(err) {
			 	     console.log(err); // 请求失败的回调函数，输出错误信息
			 	   }
			 })
			return that.messageData
		 },
		getDeviceWarnMsg(){
			if(!this.$util.getRights().pd_device_repair) return
			if(this.isAll) return
			uni.showLoading({
			    title: '加载中'
			});
			this.$api.baseData_getPdDevicAlarm({
				start: this.page * this.limit,
				limit: this.limit,
				alarm_status: '0'
			}).then((res)=>{

				if(this.page > 0){
					if(res.msg_info.length < this.limit){
						this.isAll = true
					}
					this.deviceMsgList = this.deviceMsgList.concat((res.msg_info))
				}else{
					this.deviceMsgList = res.msg_info
				}
				uni.hideLoading();
				this.page++
			})
		},
		handleMsg(msgId, result){
			this.$util.confirm({
				content: "确定" + (result == '0' ? '忽略' : '响应') +"该告警信息？",
				confirmText: "确定"
			}).then(isConfirm => {
				if (isConfirm) {
					this.$util.post({
						url: "pdDeviceAlarm/reply",
						data: {
							alarm_id: msgId,
							result: result,
						}
					}).then((res)=>{
						this.page = 0;
						this.isAll = false
						this.getDeviceWarnMsg()
					});
				}
			})

		},
		menuClick(item) {
			if (!item.url) {
				this.$util.noOpen()
				return;
			}
			uni.navigateTo({
				url: item.url
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.util-bar{
	position: sticky;
	top: calc(var(--status-bar-height) + 90rpx);
	z-index: 999;
}
.scroll-item{
	display: flex;
	align-items: center;
	gap: 10rpx;
	margin-top: 12rpx;
}

.sub{
	color: #999999;
	font-size: 30rpx;
}
.search-area{
	background-color: #FFFFFF;
	padding: 20rpx 30rpx;
	.search-input{
		height: 80rpx;
		background: #F8F8F8;
		border-radius: 10rpx;
		display: flex;
		flex: 1;
		align-items: center;
		.search-icon{
			width: 48rpx;
			height: 48rpx;
			margin: 0 20rpx 0 30rpx;
		}
		input{
			height: 80rpx;
			font-size: 28rpx;
			flex: 1;
		}
	}
}
.menu {
	background-color: #FFFFFF;
	padding: 17px 30rpx;
	.menu-line{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.menu-item {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		flex: 1;
		position: relative;
		.menu-item-img {
			width: 80rpx;
			height: 80rpx;
			margin-bottom: 10rpx;
		}
		.menu-num{
			width: 30rpx;
			height: 30rpx;
			background-color: #FF3737;
			color: #FFFFFF;
			font-size: 22rpx;
			border-radius: 30rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: absolute;
			top: 0;
			right: 40rpx;
		}
		.text{
			font-size: 28rpx;
			color: #333333;
		}
	}
}
.info-list{
	margin: 20rpx 30rpx 0;
	padding-bottom: 30rpx;

	.info-item{
		background-color: #FFFFFF;
		border-radius: 10rpx;
		padding: 30rpx 20rpx 0;
		margin-bottom: 20rpx;
		.title{
			display: flex;
			justify-content: space-between;
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
			image{
				width: 48rpx;
				height: 48rpx;
			}
		}
		.sub-title{
			font-size: 32rpx;
			font-weight: bolder;
			color: #333333;
			margin: 30rpx 0 10rpx;
		}
		.content{
			font-size: 28rpx;
			color: #666666;
			.content-item{
				display: flex;
				font-size: 28rpx;
				color: #333333;
				line-height: 50rpx;
				.key{
					min-width: 170rpx;
					font-size: 28rpx;
					font-weight: normal;
					color: #999999;
				}
			}
			.red{
				font-size: 28rpx;
				font-weight: bolder;
				color: #FF2929;
			}
		}
		.bottom{
			margin-top: 30rpx;
			border-top: 1px solid #F4F4F4;
			display: flex;
			justify-content: space-between;
			height: 97rpx;
			align-items: center;
			font-size: 28rpx;
			color: #999999;
			.btn{
				display: flex;
				.common-btn{
					height: 60rpx;
					font-size: 24rpx;
					padding: 0 30rpx;
					margin: 0;
				}
				.btn-plant{
					margin-right: 20rpx;
				}
			}
		}
	}
}
</style>
